#include "Avalon/UidServiceTests/MockUidDataStore.hpp"

using namespace Avalon;
using namespace Avalon::UidService;
using namespace Avalon::UidService::Tests;
using namespace boost;
using namespace boost::signals2;
using namespace boost::posix_time;

MockUidDataStore::MockUidDataStore()
    : m_isConnected(false),
      m_nextUid(1) {}

MockUidDataStore::~MockUidDataStore() {}

long long MockUidDataStore::GetNextUid() {
  boost::lock_guard<boost::mutex> lock(m_mutex);
  return m_nextUid;
}

long long MockUidDataStore::Reserve(long long size) {
  boost::lock_guard<boost::mutex> lock(m_mutex);
  long long nextUid = m_nextUid;
  m_nextUid += size;
  return nextUid;
}

void MockUidDataStore::Connect() {
  boost::lock_guard<boost::mutex> lock(m_mutex);
  if(m_isConnected) {
    return;
  }
  m_isConnected = true;
}

bool MockUidDataStore::IsConnected() const {
  boost::lock_guard<boost::mutex> lock(m_mutex);
  return m_isConnected;
}

void MockUidDataStore::Close() {
  boost::lock_guard<boost::mutex> lock(m_mutex);
  if(!m_isConnected) {
    return;
  }
  m_isConnected = false;
  m_closedSignal();
}

connection MockUidDataStore::ConnectClosedSignal(
    const ClosedSignal::slot_type& slot) const {
  boost::lock_guard<boost::mutex> lock(m_mutex);
  return m_closedSignal.connect(slot);
}

connection MockUidDataStore::ConnectClosedSignal(
    const ClosedSignal::slot_type& slot, Out<bool> isConnected) const {
  boost::lock_guard<boost::mutex> lock(m_mutex);
  *isConnected = m_isConnected;
  return m_closedSignal.connect(slot);
}
